package com.maoslv.controller;

import cn.hutool.core.map.MapUtil;
import com.maoslv.aop.LogAnnotation;
import com.maoslv.dto.Result;
import com.maoslv.pojo.Menu;
import com.maoslv.pojo.User;
import com.maoslv.service.menu.MenuService;
import com.maoslv.service.user.UserService;
import com.maoslv.utils.RedisUtil;
import com.maoslv.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@Scope("request")
public class MenuController {
    @Autowired
    MenuService menuService;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/list")
    @LogAnnotation(operator = "获取菜单列表")
    public Result getMenuList(Principal principal) {
        String username = principal.getName();
        User byUsername = userService.getByUsername(principal.getName());
        if (redisUtil.get(username + "-menuList") == null) {
            List<Menu> menuList = menuService.getMenuList(byUsername.getId());
            redisUtil.set(username + "-menuList", menuList);
            return Result.succ("获取菜单成功", TreeUtil.toTree(menuList));
        } else {
            return Result.succ("获取菜单成功", TreeUtil.toTree((List<Menu>) redisUtil.get(username + "-menuList")));
        }
    }

    @GetMapping("/treeList")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    @LogAnnotation(operator = "获取树形菜单列表")
    public Result getTreeList() {
        List<Menu> menuList = menuService.getMenuList(1);
        List<Menu> treeList = TreeUtil.toTree(menuList);
        return Result.succ("获取树形菜单成功",treeList);
    }

    @GetMapping("/menuIdList/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    @LogAnnotation(operator = "获取菜单ID列表")
    public Result getMenuIdList(@PathVariable Integer id) {
        List<Integer> menuList = menuService.getMenuListById(id);
        if (menuList != null) {
            return Result.succ("获取菜单Id列表成功", MapUtil.builder()
                                                            .put("menuList",menuList)
                                                            .put("id",id)
                                                            .map());
        }
        return Result.fail("获取失败");
    }

    @GetMapping("/getAllMenuList")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    @LogAnnotation(operator = "获取全部菜单列表")
    public Result getAllMenuList() {
        List<Menu> allMenuList = menuService.getAllMenuList();
        if (allMenuList != null) {
            return Result.succ("获取成功",allMenuList);
        }
        return Result.fail("获取失败");
    }

    @PostMapping("addMenu")
    @PreAuthorize("hasAnyAuthority('sys:menu:add')")
    @LogAnnotation(operator = "新增菜单")
    public Result addMenu(@RequestBody Menu menu, Principal principal) {
        if (menu.getParentId() == 0 && menu.getType() != 1) {
            return Result.fail("上级菜单和类型不规范");
        }
        if (menu.getParentId() != 0) {
            if (menuService.getType(menu.getParentId()) >= menu.getType()) {
                return Result.fail("上级菜单和类型不规范");
            }
        }

        if (menuService.addMenu(menu, principal) > 0) {
            return Result.succ("添加成功", null);
        }
        return Result.fail("添加失败");
    }

    @GetMapping("delMenu/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:del')")
    @LogAnnotation(operator = "删除菜单")
    public Result delMenu(@PathVariable Integer id, Principal principal) {
        if (menuService.haveChild(id)) {
            return Result.fail("请先删除子菜单");
        }

        if (menuService.delMenu(id, principal) > 0) {
            return Result.succ("删除成功",null);
        }

        return Result.fail("删除失败");
    }

    @GetMapping("getInfo/{id}")
    public Result getInfo(@PathVariable Integer id) {
        Menu menu = menuService.getInfo(id);
        if (menu != null) {
            return Result.succ("获取成功", menu);
        }
        return Result.fail("获取失败");
    }

    @PostMapping("updateMenu")
    @PreAuthorize("hasAnyAuthority('sys:menu:update')")
    @LogAnnotation(operator = "编辑菜单")
    public Result updateMenu(@RequestBody Menu menu, Principal principal) {
        if (menu.getParentId() == 0 && menu.getType() != 1) {
            return Result.fail("上级菜单和类型不规范");
        }
        if (menu.getParentId() != 0) {
            if (menuService.getType(menu.getParentId()) >= menu.getType()) {
                return Result.fail("上级菜单和类型不规范");
            }
        }
        Map<String, Object> menuMap= new HashMap<>();
        menuMap.put("parentId", menu.getParentId());
        menuMap.put("name", menu.getName());
        menuMap.put("perms", menu.getPerms());
        menuMap.put("path", menu.getPath());
        menuMap.put("icon", menu.getIcon());
        menuMap.put("type", menu.getType());
        menuMap.put("id", menu.getId());
        if (menuService.updateMenu(menuMap, principal) > 0) {
            return Result.succ("更新成功",null);
        }
        return Result.fail("更新失败");
    }
}
